package com.hzqy.service.workOrders;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.workOrders.WorkOrderDao;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.CmPicturesVo;
import com.hzqy.web.vo.PmsOrgVo;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.PmsZTreeVo;
import com.hzqy.commons.utils.FileUtils;
import com.hzqy.commons.utils.PublicUtil;
import com.hzqy.service.picture.PictureType;
import com.hzqy.service.pms.PmsServiceBean;
import com.hzqy.web.vo.advertisement.AdvertisementViewVo;
import com.hzqy.web.vo.advertisement.AdvertisementVo;
import com.hzqy.web.vo.advertisement.CmContentAdvertisementVo;
import com.hzqy.web.vo.workOrder.WorkOrderVo;

@Service
public class WorkOrderServiceBean {
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());

	@Resource
	private WorkOrderDao workOrderDao;

	@Resource
	private PictureDao pictureDao;
	@Resource
	private PmsServiceBean pmsServiceBean;

	public Map<String,Object> insertWorkOrder(WorkOrderVo workOrderVo,String filePath){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = 0;
		if(workOrderVo==null || workOrderVo.getF_wo_projectName()==null) {
			flag = -1;
		}
		if(flag==0){
			workOrderDao.insertWorkOrder(workOrderVo);
			if(!"".equals(filePath)){
				int f_wo_id = workOrderVo.getF_wo_id();
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo.setF_cp_relationid(f_wo_id);
				cmPicturesVo.setF_cp_url(filePath);
				cmPicturesVo.setF_cp_cprid(37);
				pictureDao.insertPic(cmPicturesVo);
			}
			resultMap.put("success", true);
			resultMap.put("result","新增工单成功!");
		}
		return resultMap;
	}

	public String getFilePath(String picType) {
		//增加了imgFile变量的前缀PIC/，因为所有的图片需要放到另一个工程中，要做一个分类。有可能会影响环球剧场，注意
		String imgFile = "PIC/imgs/upload/"+PictureType.getPicType(picType)+"/"+PublicUtil.getCurDate()+"/";
		return imgFile;
	}

	public String getFileName(String uploadifyFileName) {
		/*Random random = new Random();*/
		SecureRandom random = new SecureRandom();
		int ranValue = random.nextInt(10000);
		String fileName = PublicUtil.getGenFileName()+ranValue+"."+FileUtils.getExtendName(uploadifyFileName);
		return fileName;
	}
	/**
	 * 列表获取
	 * @param userVo
	 * @param WorkOrderVo
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public Map<String,Object> selectWorkOrderList(PmsUserVo userVo, WorkOrderVo workOrderVo, int curPage,int pageSize) {
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了 
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			workOrderVo.setF_wo_orgs(orgIds);
		}
		//LOG.debug("用户对应的组织列表:"+workOrderVo.getF_wo_orgs());	
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = workOrderDao.findWorkOrderCount(workOrderVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_wo_address", workOrderVo.getF_wo_address());
			map.put("f_wo_projectName", workOrderVo.getF_wo_projectName());
			map.put("f_wo_status", workOrderVo.getF_wo_status());
			map.put("f_wo_startTime", workOrderVo.getF_wo_startTime());
			map.put("f_wo_endTime", workOrderVo.getF_wo_endTime());
			LOG.debug("f_wo_createUserId="+ workOrderVo.getF_wo_createUserId());
			map.put("f_wo_createUserId", workOrderVo.getF_wo_createUserId());
			map.put("f_wo_orgs", workOrderVo.getF_wo_orgs());
			List<CmContentVo> results = workOrderDao.findWorkOrderList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<WorkOrderVo>());
		}
		return map;
	}
	public Integer reviewWorkOrder(WorkOrderVo workOrderVo) {
		Integer results = workOrderDao.reviewWorkOrder(workOrderVo);
		return results;
	}

	public WorkOrderVo selectWorkOrder(WorkOrderVo workOrderVo) {
		return  workOrderDao.selectWorkOrder(workOrderVo);
	}
	public int updateWorkOrder(WorkOrderVo workOrderVo,String filePath,String picType,int picTypeId) {
		int count;
		if(workOrderVo.getF_wo_type()==1){
			count = workOrderDao.updateWorkOrder(workOrderVo);
			int f_wo_id = workOrderVo.getF_wo_id();
			Map<String,Object> picMap = new HashMap<String, Object>();
			picMap.put("f_cp_relationid", f_wo_id);
			picMap.put("picType", picType);
			pictureDao.deletePic(picMap);
			if(!"".equals(filePath)){
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo.setF_cp_relationid(f_wo_id);
				cmPicturesVo.setF_cp_url(filePath);
				cmPicturesVo.setF_cp_cprid(picTypeId);
				int acout = pictureDao.insertPic(cmPicturesVo);
			}
		}else{
			count = workOrderDao.updateWorkOrderTwo(workOrderVo);
		}
		return  count ;
	}
	public int deleteWorkOrder(int f_wo_id) {
		return workOrderDao.deleteWorkOrder(f_wo_id);
	}
}
